/* 9.1
 * merge two arrays to one sorted array
 */
class Merge{
	// private int[] a;
	// private int[] b;
	public Merge(){
	}
	
	public void merge(int[] a, int[] b, int n, int m){
		int k = m + n - 1; // index of last location of a
		int i = n - 1; // index of last element in array a
		int j = m - 1; // index of last element in array b
		
		while ( i >= 0 && j >= 0 ) {
			if(a[i] > b[j])
				a[k--] = a[i--];
			else
				a[k--] = b[j--];
		}
		while( j >= 0 ) { // add left b[] in the front for they are all smaller than a[0]
			a[k--] = b[j--];
		}
//		while (i>= 0) keep the same because the left a[] are all smaller than b[0]
	}
	public static void main(String[] args){
		// int i = 10;
		// while(i>=0){
		// 	System.out.println(i--);
		// }
		//System.out.printf("End of while loop, i is %d", i);
		Merge merge = new Merge();
 		int[] a = new int[256];
		a[0] = 2;
		a[1] = 3;
		a[2] = 4;
		a[3] = 6;
		a[4] = 16;
		a[5] = 19;
		int[] b = new int[]{5,8,9,12,14,16};		
		merge.merge(a, b, 6, 6);
		
		//Tester
		for (int i = 0; i < 12; i++){
			System.out.println(a[i]);
		}
	}	
}